{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Numpy中的轴",
   "id": "dcc1ef63ceaf61c3"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1. 沿轴切片",
   "id": "cb81457b4157ba0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T06:16:48.397651Z",
     "start_time": "2025-08-28T06:16:48.370557Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([  [1,2,3] , [4,5,6] , [7,8,9]  ])\n",
    "print(arr1)\n",
    "print(arr1.shape)\n",
    "print('=' * 30)\n",
    "\n",
    "print(arr1[:2])\n",
    "\n",
    "print('=' * 30)\n",
    "print(arr1[:2,1:])"
   ],
   "id": "1a65cdba0d069601",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "(3, 3)\n",
      "==============================\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "==============================\n",
      "[[2 3]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. 传入轴的编号",
   "id": "f69fd4ac9a8ec986"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T06:47:42.921642Z",
     "start_time": "2025-08-28T06:47:42.917015Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr2 = np.arange(16).reshape((2, 2, 4))\n",
    "print(arr2)\n",
    "print(arr2.shape)\n",
    "\n",
    "# 维度是2 2 4  对应的索引  0 1 2\n",
    "print('=' * 30)\n",
    "\n",
    "\n",
    "\n",
    "# arr3 = arr2.transpose(1,0,2)\n",
    "# print(arr3)\n",
    "\n",
    "arr3 = arr2.transpose(0,2,1)\n",
    "print(arr3)\n",
    "\n"
   ],
   "id": "158d109bd8a5627a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [12 13 14 15]]]\n",
      "(2, 2, 4)\n",
      "==============================\n",
      "[[[ 0  4]\n",
      "  [ 1  5]\n",
      "  [ 2  6]\n",
      "  [ 3  7]]\n",
      "\n",
      " [[ 8 12]\n",
      "  [ 9 13]\n",
      "  [10 14]\n",
      "  [11 15]]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3. 沿轴的操作\n",
    "### 3.1 聚合函数(Aggregation)\n",
    "- 对哪个axis进行操作，就会压缩(消除)哪个维度\n"
   ],
   "id": "c65e61d024dc316b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T07:12:05.941330Z",
     "start_time": "2025-08-28T07:12:05.937308Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr4 = np.array([\n",
    "    [1,2,3],\n",
    "    [4,5,6],\n",
    "])\n",
    "# 沿着 0 轴求和\n",
    "print(arr4.sum(axis=0)) # [1+4  2+5  3+6]\n",
    "# 沿着1轴求和\n",
    "print(arr4.sum(axis=1)) # [1+2+3   4+5+6]"
   ],
   "id": "6f029f9988e3df39",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[ 6 15]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T07:17:27.085829Z",
     "start_time": "2025-08-28T07:17:27.080847Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr5 = np.ones((2,3,4))\n",
    "print(arr5)\n",
    "\n",
    "print('=' * 30)\n",
    "print(arr5.sum(axis=0))  # 二维数组(3,4)，所有元素都是二\n",
    "print('=' * 30)\n",
    "print(arr5.sum(axis=1))  # 二维数组（2,4） 所有元素都是3\n",
    "print('=' * 30)\n",
    "print(arr5.sum(axis=2)) # 二维数组（2，3）,所有元素都是4"
   ],
   "id": "eb0ec97dbbe77494",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]]\n",
      "\n",
      " [[1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]]]\n",
      "==============================\n",
      "[[2. 2. 2. 2.]\n",
      " [2. 2. 2. 2.]\n",
      " [2. 2. 2. 2.]]\n",
      "==============================\n",
      "[[3. 3. 3. 3.]\n",
      " [3. 3. 3. 3.]]\n",
      "==============================\n",
      "[[4. 4. 4.]\n",
      " [4. 4. 4.]]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 3.2. 连接函数(Concatenation)\n",
    "- 沿着哪个axis连接，就在哪个维度上增加大小\n"
   ],
   "id": "899f02c70c9a49cb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T07:22:51.571970Z",
     "start_time": "2025-08-28T07:22:51.567503Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([\n",
    "    [1,2],\n",
    "    [3,4]\n",
    "])\n",
    "\n",
    "b = np.array([\n",
    "    [5,6],\n",
    "    [7,8]\n",
    "])\n",
    "\n",
    "c = np.concatenate((a,b),axis=1) # 沿行添加\n",
    "print(c)\n",
    "print('=' * 30)\n",
    "d = np.concatenate((a,b),axis=0) #沿列添加\n",
    "print(d)"
   ],
   "id": "d7319e90e2d4be5a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n",
      "==============================\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4. Numpy数组轴的转置",
   "id": "b8729db8fd64e6c7"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4.1 reshape() 用法",
   "id": "88220194f35960e0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T07:26:35.846047Z",
     "start_time": "2025-08-28T07:26:35.841986Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr6 = np.arange(1,7)\n",
    "print(arr6)\n",
    "print('=' * 30)\n",
    "\n",
    "# 轴的转置\n",
    "arr7 = arr6.reshape(3,2)\n",
    "print(arr7)\n",
    "\n",
    "print('='*30)\n",
    "print(arr7.T)"
   ],
   "id": "452205db8515fad8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "==============================\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "==============================\n",
      "[[1 3 5]\n",
      " [2 4 6]]\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "###  4.2 transpose方法【行列转置】\n",
    "- 不传参，就是行和列的转换"
   ],
   "id": "f9e43f10265c01b8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T07:29:45.537395Z",
     "start_time": "2025-08-28T07:29:45.532469Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr8 = np.arange(24).reshape((4,6))\n",
    "print(arr8)\n",
    "print(\"-\"*30)\n",
    "print(arr8.transpose()) #\n"
   ],
   "id": "31c0e36eb7d352b2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "------------------------------\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 4.3 swapaxes方法【轴转置】\n",
    "- swapaxes() 函数是对称操作，交换轴0和轴1与交换轴1和轴0是等价的，就像数学中的交换律一样。"
   ],
   "id": "2c3cbcc7b54129ce"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T07:33:18.488630Z",
     "start_time": "2025-08-28T07:33:18.475071Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr9 = np.arange(24).reshape((4,6))\n",
    "print(arr9)\n",
    "print(\"-\"*30)\n",
    "print(arr9.swapaxes(1,0))\n"
   ],
   "id": "da3ec66c80d14947",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "------------------------------\n"
     ]
    },
    {
     "ename": "AxisError",
     "evalue": "axis1: axis 2 is out of bounds for array of dimension 2",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAxisError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[45], line 4\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28mprint\u001B[39m(arr9)\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m-\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;241m*\u001B[39m\u001B[38;5;241m30\u001B[39m)\n\u001B[1;32m----> 4\u001B[0m \u001B[38;5;28mprint\u001B[39m(arr9\u001B[38;5;241m.\u001B[39mswapaxes(\u001B[38;5;241m2\u001B[39m,\u001B[38;5;241m3\u001B[39m))\n",
      "\u001B[1;31mAxisError\u001B[0m: axis1: axis 2 is out of bounds for array of dimension 2"
     ]
    }
   ],
   "execution_count": 45
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
